//
//  top-upVC.m
//  jiaKaoZhuShou
//
//  Created by 金艺社 on 2018/5/23.
//  Copyright © 2018年 金艺社. All rights reserved.
//

#import "topUpVC.h"
#import "chongZhiTiXianRecordVC.h"
#import <AlipaySDK/AlipaySDK.h>
#import "GKCustomAlertView.h"
#import "ResetLoginPasswordVC.h"
#import "resultVC.h"
#import "setTradingPassword.h"
@interface topUpVC ()
@property (nonatomic, weak) GKCustomAlertView *alertV;
@end

@implementation topUpVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.WithdrawalRules_tv.selectable = NO;
    self.confirm_btn.layer.masksToBounds = YES;
    self.confirm_btn.layer.cornerRadius = 20;
    if ([self.objectId isEqualToString:@"1"]) {
        self.title = @"充值";
    }else {
        self.title = @"提现";
        self.accountBalance_lab.text = @"可提现余额";
        self.accountBalanceContent_lab.text = @"0.00";
        self.Top_upAmountTitle_lab.text = @"提现金额";
        self.Top_upAmountContent_lab.placeholder = @"请输入提现金额（最低100元）";
        [self.confirm_btn setTitle:@"确认提现" forState:0];
        [self WithdrawalRulesRequest];
    }
    [self addRightBtnItem];
}
- (void)addRightBtnItem {
    NSString *title = [NSString stringWithFormat:@"%@%@", self.title, @"记录"];
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(tapRightButton:)];
    self.navigationItem.rightBarButtonItem = rightBarItem;
}
- (void)tapRightButton:(id)sender {
    chongZhiTiXianRecordVC *VC = [chongZhiTiXianRecordVC new];
    if ([self.objectId isEqualToString:@"1"]) {
        VC.title = @"充值记录";
    }else {
        VC.title = @"提现记录";
    }
    [self.navigationController pushViewController:VC animated:YES];
}
- (IBAction)confirmClick:(id)sender {
    if ([self.title isEqualToString:@"充值"]) {
        [self rechargeRequest];
    }else {
        if ([self isCanCash]) {
            __weak typeof(self)weakSelf = self;
            self.alertV = [GKCustomAlertView showTradePasswordAlertWithMessage:@"提现确认" completionBlock:^(GKCustomAlertView *alertView, NSInteger buttonIndex) {
                if (buttonIndex == 1) {
                    if (![alertView.passwordView.passwordTF.text isEqualToString:@""]) {
                        [weakSelf withdrawalRequesWith:alertView.passwordView.passwordTF.text];
                        [alertView dismissAlertView];
                    }else {
                        [MBProgressHUD gk_toastShowMessage:@"请输入交易密码"];
                    }
                }else if (buttonIndex == 2) {
                    [alertView dismissAlertView];
                    ResetLoginPasswordVC *VC = [ResetLoginPasswordVC new];
                    VC.title = @"重置交易密码";
                    [self.navigationController pushViewController:VC animated:YES];
                }
            }];
        }
    }
}

#pragma mark --- 充值请求后台
- (void)rechargeRequest {
    NSDictionary *dic = @{
                          @"token":USERTOKEN,
                          @"money":self.Top_upAmountContent_lab.text
                          };
    __weak typeof(self) weakSelf = self;
    NSString *url = [NSString stringWithFormat:@"%@", [[apiManger shareApiManger] recharge]];
    [networkManger PostHttpDataWithUrlStr:url Dic:dic SuccessBlock:^(id responseObject) {
        if ([responseObject[@"rcd"] isEqualToString:R0001]) {
            [weakSelf playClickWithOrder:responseObject [@"data"]];
        }else if ([responseObject[@"rmg"] isEqualToString:@"用户不存在"]) {
           [MBProgressHUD gk_toastShowMessage:@"请先登录"];
        }else{
           [MBProgressHUD gk_toastShowMessage:responseObject[@"rmg"]];
        }
    } FailureBlock:^(id error) {
        [MBProgressHUD gk_toastShowMessage:@"请求失败"];
    }];
}

- (void)playClickWithOrder:(NSString *)order {
    [[AlipaySDK defaultService] payOrder:order fromScheme:AppScheme callback:^(NSDictionary *resultDic) {
        NSLog(@"reslut = %@",resultDic);
    }];
}

#pragma mark --- 提现
// 提现规则 / 充值余额获取
- (void)WithdrawalRulesRequest {
    NSDictionary *dic = @{
                          @"token":USERTOKEN,
                          };
    __weak typeof(self) weakSelf = self;
    NSString *url = [NSString stringWithFormat:@"%@", [[apiManger shareApiManger] WithdrawalRules]];
    [networkManger PostHttpDataWithUrlStr:url Dic:dic SuccessBlock:^(id responseObject) {
        if ([responseObject[@"rcd"] isEqualToString:R0001]) {
            weakSelf.accountBalanceContent_lab.text = responseObject[@"cashMoney"];
            if ([self.title isEqualToString:@"提现"]) {
                NSString *ruals = [NSString stringWithFormat:@"1.每笔收取手续费%@元.\n2.单笔提现最低100元，最高50000元.", responseObject[@"feeFixed"]];
                weakSelf.WithdrawalRules_tv.text = ruals;
            }
        }else {
            [MBProgressHUD gk_toastShowMessage:responseObject[@"rmg"]];
            [self pushSetTradView];
        }
    } FailureBlock:^(id error) {
        [MBProgressHUD gk_toastShowMessage:@"请求失败"];
    }];
}
- (void)pushSetTradView {
    setTradingPassword *VC = [setTradingPassword new];
    [self.navigationController pushViewController:VC animated:YES];
}
// 提现请求
- (void)withdrawalRequesWith:(NSString *)s {
    self.confirm_btn.userInteractionEnabled = NO;
    NSDictionary *dic = @{
                          @"token":USERTOKEN,
                          @"cashMoney":self.Top_upAmountContent_lab.text,
                          @"safepwd":s
                          };
    NSString *url = [[apiManger shareApiManger] WithdrawalRequest];
    [networkManger PostHttpDataWithUrlStr:url Dic:dic SuccessBlock:^(id responseObject) {
         self.confirm_btn.userInteractionEnabled = YES;
        if ([responseObject[@"rcd"] isEqualToString:R0001]) {
            resultVC *VC = [resultVC new];
            VC.withdraw = self.Top_upAmountContent_lab.text;
            VC.resultType = @"9";
            [self.navigationController pushViewController:VC animated:YES];
        }else{
            [MBProgressHUD gk_toastShowMessage:responseObject[@"rmg"]];
        }
    } FailureBlock:^(id error) {
        self.confirm_btn.userInteractionEnabled = YES;
    }];
}
- (BOOL)isCanCash {
    if (self.Top_upAmountContent_lab.text.doubleValue > self.accountBalanceContent_lab.text.doubleValue) {
        [MBProgressHUD gk_toastShowMessage:@"提现金额多于账户余额"];
        return NO;
    }else if (self.Top_upAmountContent_lab.text.doubleValue < 100) {
        [MBProgressHUD gk_toastShowMessage:@"提现金额少于100元"];
        return NO;
    }else if (self.Top_upAmountContent_lab.text.doubleValue > 50000) {
        [MBProgressHUD gk_toastShowMessage:@"提现金额多于50000元"];
        return NO;
    }
    return YES;
}
- (void)dealloc {
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
